#include "imageContainer.h"

ImageContainer::ImageContainer(QWidget *_parent) : QWidget(_parent){
    QWidget::setAttribute(Qt::WA_DeleteOnClose);

    isUntitled = true;
    image = new Image(this);
    pathName = QString::fromUtf8("Sem título");

    histogram = NULL;


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(image);

    setLayout(mainLayout);
}

ImageContainer::~ImageContainer(){
    if (image) delete image;
    if (histogram) delete histogram;
}

bool ImageContainer::loadImage(const QString &_fileName){
    setCurrentImage(_fileName);

    update();

    if(image) return true;
    else return false;
}

bool ImageContainer::save(){
    if (isUntitled){
        return saveAs();
    }

    image->getImage()->save(pathName);

    setWindowModified(false);

    return true;
}

bool ImageContainer::saveAs(){
    QFileDialog dlg;
    dlg.setDefaultSuffix("jpg");
    pathName = dlg.getSaveFileName(this, tr("Escolha o nome do arquivo"), QDir::currentPath());

    if (pathName.isEmpty()) return false;


    if (QFileInfo(pathName).suffix().isEmpty())
        pathName += ".jpg";

    image->getImage()->save(pathName);



    setWindowModified(false);
    return true;
}

void ImageContainer::normalSize(){
    image->normalSize();
}

void ImageContainer::zoomIn(){
    image->zoomIn();
}

void ImageContainer::zoomOut(){
    image->zoomOut();
}

void ImageContainer::fitToWindow(){
    image->fitToWindow();
}

QImage *ImageContainer::getImage(){
    return image->getImage();
}

QString ImageContainer::getName(){
    return QFileInfo(pathName).fileName();
}

Histogram *ImageContainer::getHistogram(){
    return histogram;
}

void ImageContainer::update(QImage *_img){
    if (_img){
        if(isUntitled){
            pathName = QString::fromUtf8("Sem título");
            setWindowTitle(QFileInfo(pathName).fileName() + "[*]");
            histogram = new Histogram(new Data(_img->bits(), _img->width(), _img->height()));
        }else{
            histogram->calculate(new Data(_img->bits(), _img->width(), _img->height()));
        }
        setWindowModified(true);
        image->update(_img);

    }
    else image->update();
}

void ImageContainer::closeEvent(QCloseEvent *event){
    if (maybeSave()) {
        event->accept();
    } else {
        event->ignore();
    }
}

bool ImageContainer::maybeSave(){
    if (isWindowModified()) {
        QMessageBox::StandardButton ret;
        ret = QMessageBox::warning(this, QString::fromUtf8("Alterações não salvas"),
            QString::fromUtf8("'%1' foi alterada.\n"
            "Deseja salvar as alterações?")
            .arg(QFileInfo(pathName).fileName()),
            QMessageBox::Save | QMessageBox::Discard
            | QMessageBox::Cancel);

        if (ret == QMessageBox::Save)
            return save();
        else if (ret == QMessageBox::Cancel)
            return false;
    }
    return true;
}

void ImageContainer::setCurrentImage(const QString &_fileName){
    QImage *img = new QImage(_fileName);
    image->update(img);
    histogram = new Histogram(new Data(img->bits(), img->width(), img->height()));

    pathName = QFileInfo(_fileName).canonicalFilePath();
    isUntitled = false;

    setWindowTitle(QFileInfo(pathName).fileName() + "[*]");
    setWindowModified(false);
}
